#include "dlg_addstu.h"
#include "stumysql.h"
#include "ui_dlg_addstu.h"
Dlg_AddStu::Dlg_AddStu(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_AddStu)
{
    ui->setupUi(this);
}

Dlg_AddStu::~Dlg_AddStu()
{
    delete ui;
}

void Dlg_AddStu::setType(bool isAdd, stuInfo info)
{
    m_isAdd = isAdd;
    m_info = info;
    ui->le_name->setText(info.name);
    ui->sp_age->setValue(info.age);
    ui->le_grade->setText(QString::number(info.grade));
    ui->le_uiclass->setText(QString::number(info.uiclass));
    ui->le_studentid->setText(QString::number(info.studentid));
    ui->le_phone->setText(info.phone);
    ui->le_wechat->setText(info.wechat);
}

void Dlg_AddStu::on_btn_cancel_clicked()
{
    this->hide();
    //this->window()->close();//这句话也是退出增加小界面,返回到主窗口
}

void Dlg_AddStu::on_btn_save_clicked()
{
    stuInfo info;
    auto ptr = stumySql::getinstance();
    info.id = m_info.id; //要修改的id传到setType就断了,给了m_info,没传到这个保存按钮,索性赋过来
    info.name = ui->le_name->text();
    info.age = ui->sp_age->text().toUInt();
    info.grade = ui->le_grade->text().toUInt();
    info.uiclass = ui->le_grade->text().toUInt();
    info.studentid = ui->le_studentid->text().toUInt();
    info.phone = ui->le_phone->text();
    info.wechat = ui->le_wechat->text();
    //ptr->addstu(info);
    if (m_isAdd) {
        ptr->addstu(info);
    } else {
        ptr->UpdateStuInfo(info);
    }
    QMessageBox::information(nullptr, "信息", "存储成功");
    this->hide();
}
